package com.lipaluma.xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.lipaluma.field.validator.strategies.ValidationStrategyEnum;
import com.lipaluma.xml.configuration.XmlConfiguration;
import com.lipaluma.xml.mapping.XmlColumnMapping;
import com.lipaluma.xml.mapping.XmlFieldMapping;
import com.lipaluma.xml.mapping.XmlMultipleColumnsMapping;
import com.lipaluma.xml.mapping.XmlMultiplePathesMapping;
import com.lipaluma.xml.mapping.XmlObjectFieldMapping;
import com.lipaluma.xml.mapping.XmlPathMapping;
import com.lipaluma.xml.merge.XmlTargetMerged;

@XmlRootElement(name="parser-configuration")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={})
public abstract class XmlMappings implements XmlMappingClass {
	
	@XmlAttribute(name="validation-strategy")
	private ValidationStrategyEnum validationStrategy;
	@XmlElement(name=XmlTargetMerged.TARGET_MERGED)
	private XmlTargetMerged targetMerged;

//	@XmlElements({
//		@XmlElement(name="csv", type=XmlCsvConfiguration.class),
//		@XmlElement(name="excel", type=XmlExcelConfiguration.class)
//	})
	
	@XmlElementWrapper(name="mappings")
	@XmlElements({
		@XmlElement(name=XmlColumnMapping.COLUMN, type=XmlColumnMapping.class),
		@XmlElement(name=XmlPathMapping.PATH, type=XmlPathMapping.class),
		@XmlElement(name=XmlMultipleColumnsMapping.MULTIPLE_COLUMNS, type=XmlMultipleColumnsMapping.class),
		@XmlElement(name=XmlMultiplePathesMapping.MULTIPLE_PATHES, type=XmlMultiplePathesMapping.class),
		@XmlElement(name=XmlObjectFieldMapping.MAP_OBJECT, type=XmlObjectFieldMapping.class)
	})
	private List<? extends XmlFieldMapping> mappings;
	
	public abstract XmlConfiguration getConfiguration();
	
	@Override
	public List<? extends XmlFieldMapping> getMappings() {
		if(mappings == null)
			return new ArrayList<XmlFieldMapping>();
		return mappings;
	}
	
	public ValidationStrategyEnum getValidationStrategy() {
		if(validationStrategy == null)
			return ValidationStrategyEnum.STOP_ON_FIRST_VALIDATION_ERROR;
		return validationStrategy;
	}
	
	@Override
	public boolean isTargetCanBeMerged() {
		return targetMerged != null;
	}
	
	@Override
	public XmlTargetMerged targetMergedBy() {
		return targetMerged;
	}
}
